$(document).ready(function() {
	$( "#createDate" ).datetimepicker();
	$( "#createDate" ).datetimepicker( "option",$.datepicker.regional[ 'zh-CN'] );
	$( "#createDate" ).change(function(){
		$( "#createDate" ).val($( "#createDate" ).val().trim()+":00");
	});
	$("#calculateProfitAmount").click(function(){
		var cost = $("#costAmount").val();
		var percent = $("#profitPercent").val();
		$("#profitAmount").val(accMul(cost,accDiv(percent,100)));
	});
	
	var dates = $( "#from, #to" ).datepicker({
			changeMonth: true,
			numberOfMonths: 1,
			onSelect: function( selectedDate ) {
				var option = this.id == "from" ? "minDate" : "maxDate",
					instance = $( this ).data( "datepicker" ),
					date = $.datepicker.parseDate(
						instance.settings.dateFormat ||
						$.datepicker._defaults.dateFormat,
						selectedDate, instance.settings );
				dates.not( this ).datepicker( "option", option, date );
			}
	});
	$( "#from, #to" ).datepicker($.datepicker.regional[ 'zh-CN'] );
	
	$("#searchButton").click(function(){
		$("#searchResultDiv").html("");
		$("#pageIndex").val("1");
		$("#searchResultDiv").load(getRootPath()+ "/MainMenu/members/searchResultPage.htm",$("#memberForm").serializeArray(),function(data){
			if(data.error!=undefined){
				alert(data.error);
			}
		});
	});
	
	$( "#editDiv" ).dialog({
		autoOpen:false,
		width:750,
		height:450,
		modal:true,
		buttons:{
			"保存修改": function() {
				
				var existEmpty=false;
				$.each($("#editMemberForm input[type='text']"),function(i,data){
					if($(data).val()=="") 
					{
						if($(data).attr("name")!="address"){
							existEmpty=true;
						}
						if(existEmpty){
							return;
						}
					}
				});
				if(!existEmpty){
					if(window.confirm("确定修改吗？")){
						$.post(getRootPath()+ "/MainMenu/members/updateMember.htm",$("#editMemberForm").serializeArray(),function(data){
							if(data.error==undefined){
								alert("修改成功");
								$( "#editDiv" ).dialog("close");
								$("#searchResultDiv").load(getRootPath()+ "/MainMenu/members/searchResultPage.htm",$("#memberForm").serializeArray());
							}else{
								alert("修改失败");
							}
						});
					}
				}else{
					alert("*项都是必须填的");
				}
			},
			"删除": function() {
				if(window.confirm("确定删除吗？")){
					$.getJSON(getRootPath()+ "/MainMenu/members/deleteMember.htm",{memberID:$("#id").val()},function(data){
						if(data.error==undefined){
							alert("删除成功");
							$( "#editDiv" ).dialog("close");
							$("#searchResultDiv").load(getRootPath()+ "/MainMenu/members/searchResultPage.htm",$("#memberForm").serializeArray());
						}else{
							alert("删除失败");
						}
					});
				}
			}
		}
	});
});
